<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\admin\model;

use api\admin\model\BandToiletModel;
use api\common\model\CommonModel;
use think\cache\driver\Redis;

class ToiletModel extends CommonModel
{
    // 可查询字段
    // protected $visible = [
    //     'id', 'start_time', 'end_time', 'man_num', 'woman_num','flow_num','titile','position','create_time',
    // ];
    //模型关联方法
    protected $relationFilter = ['articles'];

    /**
     * 基础查询
     */
    // protected function base($query)
    // {
    //     $query->alias('toilet')->where('toilet.status', 1);
    // }
    /**
     * 关联
     * @return $this
     */
    // public function articles()
    // {
    //     return $this->belongsToMany('PortalPostModel','portal_tag_post','post_id','tag_id');
    // }

    // [
    //     '3'=>'man_num',
    //     '4'=>'woman_num',
    //     '5'=>'disability_num',
    //     '6'=>'man_num',
    //     '7'=>'third_num',
    //     '8'=>'child_num',
    // ];

    /**
     * [getToiletJoinFields 获取初始化数据]
     * @Author   HUI
     * @DateTime 2018-11-27
     * @version  [version]
     * @param    string     $value [description]
     * @return   [type]            [description]
     */
    public function getToiletJoinFields($where = '')
    {
        $BandToilet = new BandToiletModel();

        $detail = $this->getToiletFields($where);
        $detail['thumbnail'] = !empty($detail['thumbnail']) ? json_decode($detail['thumbnail'], true):null;
        $detail['music_url'] = !empty($detail['music_url']) ? json_decode($detail['music_url'], true):null;

        return [
            'details' => $detail,
            'flow' => $this->getRedisData($where['id'], 1),
            'sensor' => $this->getRedisData($where['id'], 3),
            'smoke' => $this->getRedisData($where['id'], 4),
            'water' => $this->getRedisData($where['id'], 5),
            'electric' => $this->getRedisData($where['id'], 6),
            'sos' => $this->getRedisData($where['id'], 7),
            'man' => $this->getRedisData($where['id'], 2, 3, 'man'),
            'woman' => $this->getRedisData($where['id'], 2, 4, 'woman'),
            'infant' => $this->getRedisData($where['id'], 2, 5, 'infant'),
            'disability' => $this->getRedisData($where['id'], 2, 6, 'disability'),
            'third' => $this->getRedisData($where['id'], 2, 7, 'third'),
            'child' => $this->getRedisData($where['id'], 2, 8, 'child'),
        ];
    }

    /**
     * [getCloudStatus 获取运行状态]
     * @Author   HUI
     * @DateTime 2018-12-04
     * @version  [version]
     * @param    string     $value [description]
     * @return   [type]            [description]
     */
    public function getCloudStatus($mac)
    {
        if (!empty($mac)) {

            $Redis = new Redis();
            if ($Redis->get('cloud_status_' . $mac)) {
                return 1;
            } else {
                return 0;
            }
        }
    }
    /**
     * [bandDelDevice 清空旧设备]
     * @Author   HUI
     * @DateTime 2018-11-28
     * @version  [version]
     * @param    string     $value [description]
     * @return   [type]            [description]
     */
    public function bandDelDevice($value = '', $tid)
    {
        // cloud_mac 此MAC所有信息
        // cloud_mac_device 此MAC设备类型下的所有信息
        // cloud_mac_device_man 此MAC设备类型男厕下的所有信息
        // cloud_mac_device_man_mini[mac] 此MAC设备类型男厕下单设备的信息
        //
        // $ToiletTmp = ['man','woman','third','child','disability','infant'];
        // $device = [1=>'device',2=>'flow',3=>'sensor'];
        $where['t_id'] = $tid;
        switch ($value) {
            case 'man_num':
                $where = ['mac_device_type' => 2, 'tag_id' => 3];
                break;
            case 'woman_num':
                $where = ['mac_device_type' => 2, 'tag_id' => 4];
                break;
            case 'third_num':
                $where = ['mac_device_type' => 2, 'tag_id' => 7];
                break;
            case 'child_num':
                $where = ['mac_device_type' => 2, 'tag_id' => 8];
                break;
            case 'disability_num':
                $where = ['mac_device_type' => 2, 'tag_id' => 6];
                break;
            case 'infant_num':
                $where = ['mac_device_type' => 2, 'tag_id' => 5];
                break;
            // case 'flow_num':
            //     $where['mac_device_type'] = 1;
            // case 'sensor_num':
            //     $where['mac_device_type'] = 3;
                break;
            default:
                break;
        }
        $Band = new BandToiletModel();
        $Band->where($where)->delete();
    }

    /**
     * [getRedisData 获取redis数据]
     * @Author   HUI
     * @DateTime 2018-11-28
     * @version  [version]
     * @param    string     $value [description]
     * @return   [type]            [description]
     */
    public function getRedisData($t_id, $mac_device_type, $tag_id = '', $str = '')
    {
        //
        $where = ['a.t_id' => $t_id, 'a.mac_device_type' => $mac_device_type];
        if ($tag_id) {
            $where['a.tag_id'] = $tag_id;
        }
        // $Redis = new Redis();
        $BandToilet = new BandToiletModel();
        return $BandToilet->getbandTag($where);
        // if (PHP_OS == 'WINNT') {
        // } else {
        // //获取当前绑定的数据
        // $result = $Redis->get($t_id . '_' . $str . '_device');
        // if (empty($result)) {
        //     $res = $BandToilet->getbandTag($where);
        //     $Redis->set($t_id . '_' . $str . '_device', serialize($res),5);
        //     $result = serialize($res);
        // }
        //     return @unserialize($result);
        // }

    }

    /**
     * [dataInsertBand 插入绑定表]
     * @Author   HUI
     * @DateTime 2018-11-27
     * @version  [version]
     * @param    string     $value [description]
     * @return   [type]            [description]
     */
    public function dataInsertBand($data = '', $type = '', $tid = '',$master_mac = '')
    {

        $BandToilet = new BandToiletModel();

        $result = 0;
        foreach ($data as $key => $value) {


            $insert = [
                'tag_id' => $value,
                'mac' => '',
                'mac_device_type' => $type,
                't_id' => $tid,
            ];
            
            $where = $insert;
            unset($where['mac']);
            //查询是否存在该设备
            $exsit = $BandToilet->where($where)->find();
            
            if (!empty($exsit)) {
                continue;
            }else{
                if ($master_mac) {
                    $insert['master_mac'] = $master_mac;
                }
                $result = $BandToilet->insert($insert);
            }
        }

        return $result;

    }


    public function dataSaveBand($data = '', $type = '', $tid = '')
    {
        $BandToilet = new BandToiletModel();
        foreach ($data as $key => $value) {
            $save = [
                'tag_id' => $value,
                'mac' => '',
                'mac_device_type' => $type,
                't_id' => $tid,
            ];
            if (!$BandToilet->save($save)) {
                $this->error('操作失败');
            }
        }

        return 1;

    }




    /**
     * [getToiletFields 获取单个公厕信息]
     * @Author   HUI
     * @DateTime 2018-11-27
     * @version  [version]
     * @param    [type]     $id     [description]
     * @param    string     $fields [description]
     * @return   [type]             [description]
     */
    public function getToiletFields($id, $fields = '*')
    {
        if (is_array($id)) {
            return $this->field($fields)->where($id)->find();

        } else {
            return $this->field($fields)->where('id', $id)->find();
        }

    }

    /**
     * [getToiletFields 更新单个公厕信息]
     * @Author   HUI
     * @DateTime 2018-11-27
     * @version  [version]
     * @param    [type]     $id     [description]
     * @param    string     $fields [description]
     * @return   [type]             [description]
     */
    public function updateToiletFields($id, $update = [])
    {

        if (is_array($id)) {
            return $this->where($id)->update($update);
        } else {
            return $this->where('id', $id)->update($update);
        }
    }

    /**
     * [setToileTag 设置tag数据]
     * @Author   HUI
     * @DateTime 2018-12-03
     * @version  [version]
     * @param    string     $value [description]
     */
    public function setToileTag($value = '')
    {

    }

}
